package servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class uploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*BufferedReader reader = new BufferedReader(request.getReader());
        String boundary = reader.readLine();
        String content = reader.readLine();
        //System.out.println(getFileName(content));
        reader.readLine();
        reader.readLine();
        FileOutputStream out = new FileOutputStream("c:/ch7/WebFile/"+getFileName(content));
        while(true) {
            String str = reader.readLine();
            if(!str.equals(boundary+"--")) {
                System.out.println(str);
            }
            else
                break;
        }
        out.close();
        reader.close();
         */

        /*
        int contentLength = request.getContentLength();
        System.out.println(contentLength);
        byte[] buffer = new byte[contentLength];
        for (int i = 0; i < contentLength;) {
            int readlen = request.getInputStream().read(buffer, i, contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        BufferedReader reader = new BufferedReader(new StringReader(new String(buffer)));
        String boundary = reader.readLine();
        String content = reader.readLine();
        String type = reader.readLine();
        System.out.println(boundary);
        int start = (new String(buffer).indexOf(type)+type.length());
        int end = (new String(buffer).lastIndexOf(boundary));
        FileOutputStream out = new FileOutputStream("c:/ch7/WebFile/test.txt");
        out.write(buffer,start+4,(end-start)-6);
        out.close();
        reader.close();
         */

        int len = request.getContentLength();
        System.out.println(len);
        DataInputStream dataInputStream = new DataInputStream(request.getInputStream());
        byte[] bytes = new byte[len];
        dataInputStream.readFully(bytes);
        dataInputStream.close();

        BufferedReader reader = new BufferedReader(new StringReader(new String(bytes)));
        String boundary = reader.readLine();
        String content = reader.readLine();
        String type = reader.readLine();
        int start = (new String(bytes).indexOf(type)+type.length());
        int end = (new String(bytes).lastIndexOf(boundary));
        FileOutputStream out = new FileOutputStream("c:/ch7/WebFile"+File.separator+getFileName(content));
        out.write(bytes, start + 4,(end - start) - 6);
        out.close();
        reader.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected String getFileName(String s) {
        String filename = s.substring(s.lastIndexOf("=")+1);
        return new String(filename.split("\"")[1].getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
    }

}
